/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Historique.java
 *
 * Created on 14 mars 2012, 21:42:04
 */
package kuti.grp5.ui;

import com.sun.corba.se.spi.orbutil.fsm.Input;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 *
 * @author Jocelyn
 */
public class Historique extends JDialog {

    private final JFrame owner;

    /** Creates new form Historique */
    public Historique(JFrame owner) {
        super(owner, false);
        this.owner = owner;
        owner.setVisible(false);
        initComponents();
        initScores();
        setLocationRelativeTo(this);
        setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    /**
     * Fetch the XML scores file if it exists and shows the scores in the
     * table.
     */
    private void initScores() {
        SAXBuilder sxb = new SAXBuilder();
        Document doc = null;
        File f = new File("pantheon.xml");

        try {
            InputStream is = getClass().getResourceAsStream("/kuti/grp5/ui/resources/pantheon.xml");
            doc = f.exists() ? sxb.build(f) : sxb.build(is);
        } catch (JDOMException ex) {
            Logger.getLogger(ParamPanel.class.getName()).log(Level.SEVERE, null, ex);
            return;
        } catch (IOException ex) {
            Logger.getLogger(ParamPanel.class.getName()).log(Level.SEVERE, null, ex);
            return;
        } 
        buildTable(doc);
    }

    private void buildTable(Document doc) {
        Element root = doc.getRootElement();
        List<Element> scores = root.getChildren("score");
        String[] columns = new String[]{
            "Score", "Nom", "Date", "Difficulté"
        };
        tableModel = new DefaultTableModel(columns, 0);
        for (Element elt : scores) {
            addScoreToTable(elt);
        }
        tableHisto.setModel(tableModel);
    }

    private void addScoreToTable(Element score) {
        String pts = score.getAttributeValue("score");
        String nom = score.getAttributeValue("nom");
        String date = score.getAttributeValue("date");
        String diff = score.getAttributeValue("diff");
        tableModel.addRow(new Object[]{pts, nom, date, diff});
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        labTitre = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tableHisto = new javax.swing.JTable();
        jPanel1 = new javax.swing.JPanel();
        btClear = new javax.swing.JButton();

        setTitle("Historique");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        labTitre.setFont(new java.awt.Font("Verdana", 1, 24));
        labTitre.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        labTitre.setText("Panthéon");
        getContentPane().add(labTitre, java.awt.BorderLayout.NORTH);

        jScrollPane1.setViewportView(tableHisto);

        getContentPane().add(jScrollPane1, java.awt.BorderLayout.CENTER);

        btClear.setLabel("Effacer tout");
        btClear.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btClearActionPerformed(evt);
            }
        });
        jPanel1.add(btClear);

        getContentPane().add(jPanel1, java.awt.BorderLayout.SOUTH);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        owner.setVisible(true);
    }//GEN-LAST:event_formWindowClosing

    private void btClearActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btClearActionPerformed
        switch (JOptionPane.showConfirmDialog(this, "Voulez-vous vraiment remettre à zéro le panthéon ?")) {
            case JOptionPane.YES_OPTION:
                clearPantheon();
                break;
        }
    }//GEN-LAST:event_btClearActionPerformed

    private void clearPantheon() {
        SAXBuilder sxb = new SAXBuilder();
        Document doc = null;
        File f = new File("pantheon.xml");

        try {
            InputStream is = getClass().getResourceAsStream("/kuti/grp5/ui/resources/pantheon.xml");
            doc = f.exists() ? sxb.build(f) : sxb.build(is);
        } catch (JDOMException ex) {
            Logger.getLogger(ParamPanel.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ParamPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        Element root = doc.getRootElement();

        List<Element> scores = root.getChildren("score");

        for (Element elt : scores) {
            elt.setAttribute("date", "-");
            elt.setAttribute("nom", "-");
            elt.setAttribute("score", "0");
        }

        try {
            new XMLOutputter(Format.getPrettyFormat()).output(doc, new FileOutputStream(f));
        } catch (IOException ex) {
            Logger.getLogger(SimulationPanel.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        buildTable(doc);
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btClear;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel labTitre;
    private javax.swing.JTable tableHisto;
    // End of variables declaration//GEN-END:variables
    private DefaultTableModel tableModel;
}
